﻿Imports Ext.Net
Imports Ext.Net.MVC

Namespace DailyReport
    Public Class TransceiverController
        Inherits ControllerBase

        '
        ' GET: /Transceiver

        <Authorize()>
        Function TransceiverList() As ActionResult
            ViewData("UserID") = CurrentUserID
            ViewData("TransceiverDate") = Now

            Return View()
        End Function

        <Authorize()>
        Function TransceiverReport() As ActionResult
            ViewData("UserID") = CurrentUserID
            ViewData("TransceiverDate") = Now

            Return View()
        End Function

        <Authorize()>
        Function TransceiverDetail(ByVal userid As String, ByVal transceiverdate As Date) As ActionResult
            ViewData("UserID") = userid
            ViewData("TransceiverDate") = transceiverdate

            Return View()
        End Function


        <Authorize()>
        Public Function GetTransceiverListStore(ByVal userid As String, ByVal transceiverdate As Date) As AjaxStoreResult
            Try
                Dim startDate As Date
                Dim endDate As Date

                If Now.Day <= 27 Then
                    startDate = New Date(transceiverdate.Year, transceiverdate.Month - 1, 28)
                    endDate = Now
                Else
                    startDate = New Date(transceiverdate.Year, Now.Month, 28)
                    endDate = Now
                End If

                'Dim dclm As New DayCheckListModel '= getListModel(userid, checkdate)
                Dim tol As New List(Of Transceiver)
                Dim tl As New TransceiverListModel
                Dim t As TransceiverListItem
                Using re As New DailyReportEntitiesEX
                    tol = (From c In re.Transceiver
                          Where c.userid = userid And c.date >= startDate And c.date <= endDate
                          Select c).ToList
                End Using

                For Each c In tol
                    Dim tq = From f In tl.TransceiverListItems
                                 Where f.TransceiverDate = c.date
                                 Select f

                    If tq.Count > 0 Then
                        t = tq.First
                        t.TransceiverCount += c.count
                    Else
                        t = New TransceiverListItem
                        t.TransceiverCount += c.count
                        t.TransceiverTo = c.togroup
                        t.TransceiverDate = c.date
                        t.UserID = c.userid
                        tl.TransceiverListItems.Add(t)
                    End If
                Next

                Return New AjaxStoreResult(tl.TransceiverListItems, tl.TransceiverListItems.Count)
            Catch ex As Exception
                Return Nothing
            End Try
        End Function

        <Authorize()>
        Public Function GetTransceiverDetailStore(ByVal userid As String, ByVal transceiverdate As Date) As AjaxStoreResult
            Try
                Dim tdl As New List(Of Transceiver)

                Using re As New DailyReportEntitiesEX
                    tdl = (From c In re.Transceiver
                           Where c.userid = userid And c.date = transceiverdate
                           Select c).ToList
                End Using

                Return New AjaxStoreResult(tdl, tdl.count)
            Catch ex As Exception
                Return Nothing
            End Try
        End Function

        Public Function SaveTransceiverWithConfirmation() As AjaxStoreResult
            Dim ajaxStoreResult As New AjaxStoreResult(StoreResponseFormat.Save)
            Try
                Dim dataHandler = New StoreDataHandler(HttpContext.Request("data"))
                Dim data As ChangeRecords(Of Transceiver) = dataHandler.ObjectData(Of Transceiver)()
                Dim confirmationList As ConfirmationList = dataHandler.BuildConfirmationList("to")

                For Each cdc In data.Created
                    Using re As New DailyReportEntitiesEX
                        Dim nt As New Transceiver With {.id = System.Guid.NewGuid.ToString(),
                                                       .userid = CurrentUserID,
                                                       .togroup = cdc.togroup,
                                                       .count = cdc.count,
                                                       .date = Now.ToString("yyyy-MM-dd")}
                        re.Transceiver.AddObject(nt)
                        re.SaveChanges()
                    End Using
                Next

                For Each udc In data.Updated
                    Using re As New DailyReportEntitiesEX
                        Dim otl = (From c In re.Transceiver
                                            Where c.id = udc.id
                                            Select c)
                        Dim t As Transceiver
                        If otl.Count > 0 Then
                            t = otl.First
                            t.count = udc.count
                            t.togroup = udc.togroup
                        End If

                        re.SaveChanges()
                    End Using

                Next

                For Each c In confirmationList
                    confirmationList(c.Key).ConfirmRecord()
                Next


                ajaxStoreResult.SaveResponse.ConfirmationList = confirmationList
            Catch ex As Exception
                ajaxStoreResult.SaveResponse.Success = False
                ajaxStoreResult.SaveResponse.Message = ex.Message
            End Try

            Return ajaxStoreResult
        End Function

        <Authorize()>
        Public Function GetTransceiverReportStore(ByVal range As Integer) As AjaxStoreResult
            Try
                Dim startDate As Date
                Dim endDate As Date

                'Select Case range
                '    Case 1 '月
                startDate = New Date(Now.Year, Now.Month, 1)
                endDate = Date.Parse(Now.AddMonths(1).ToString("yyyy-MM") + "-01").AddDays(-1)
                '    Case 2 '季
                'startDate = New Date(Now.Year, Now.Month, 1)
                'endDate = Date.Parse(Now.AddMonths(1).ToString("yyyy-MM") + "-01").AddDays(-1)
                '    Case 3 '年
                'startDate = New Date(Now.Year, Now.Month, 1)
                'endDate = Date.Parse(Now.AddMonths(1).ToString("yyyy-MM") + "-01").AddDays(-1)
                'End Select
                Dim tdl As New List(Of Transceiver)
                Dim tlm As New TransceiverListModel
                Dim tlmi As TransceiverListItem

                Using re As New DailyReportEntitiesEX
                    tdl = (From c In re.Transceiver
                           Where c.date >= startDate And c.date <= endDate
                           Select c).ToList
                End Using

                For Each ot In tdl
                    Dim tq = From t In tlm.TransceiverListItems
                             Where ot.togroup = t.TransceiverTo
                             Select t

                    If tq.Count > 0 Then
                        tlmi = tq.First
                        tlmi.TransceiverCount += ot.count
                    Else
                        tlmi = New TransceiverListItem
                        tlmi.TransceiverTo = ot.togroup
                        tlmi.TransceiverCount += ot.count
                        tlm.TransceiverListItems.Add(tlmi)
                    End If
                Next

                Return New AjaxStoreResult(tlm.TransceiverListItems, tlm.TransceiverListItems.Count)
            Catch ex As Exception
                Return Nothing
            End Try
        End Function
    End Class
End Namespace